package com.tjetc.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tjetc.common.JsonResult;
import com.tjetc.dao.CaregiverInfoMapper;
import com.tjetc.dao.ElderInfoMapper;
import com.tjetc.dao.EmployeeMapper;
import com.tjetc.dao.PostInfoMapper;
import com.tjetc.entity.CaregiverInfo;
import com.tjetc.entity.ElderInfo;
import com.tjetc.entity.Employee;
import com.tjetc.entity.PostInfo;
import com.tjetc.service.ElderInfoService;
import com.tjetc.service.EmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class ElderInfoServiceImpl implements ElderInfoService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private CaregiverInfoMapper caregiverInfoMapper;
    @Autowired
    private PostInfoMapper postInfoMapper;
    @Autowired
    private ElderInfoMapper elderInfoMapper;
    @Override
    public JsonResult findById(Long id) {
        ElderInfo elderInfo=elderInfoMapper.selectById(id);
        //selectById是mybatisplus提供的单表查询操作，生成sql语句都是mybatisplus内置
        //caregiverInfo查询
        CaregiverInfo caregiver = caregiverInfoMapper.selectById(elderInfoMapper.selectCaregiverId(id));
        Employee caregiverEmployee = employeeMapper.selectById(caregiverInfoMapper.selectEmployeeId(caregiver.getCaregiverId()));
        caregiver.setEmployee(caregiverEmployee);
        //Employee查询
        Employee csManager = employeeMapper.selectById(elderInfoMapper.selectcsManagerId(id));
        PostInfo postInfocsManager = postInfoMapper.selectById(employeeMapper.selectPositionId(csManager.getEmployeeId()));
        csManager.setPosition(postInfocsManager);

        elderInfo.setCaregiver(caregiver);
        elderInfo.setCsManager(csManager);
        return JsonResult.success(elderInfo);
    }
    @Override
    public JsonResult findPage(String name, Short gender,Short healthStatus,Integer pageNo, Integer pageSize) {
        //设置页码和每页数量
        Page<ElderInfo> page=Page.of(pageNo,pageSize);
        //把adminPage传入mapper查询中，adminPage就是最后的分页结果
        Page<ElderInfo>elderInfoPage= elderInfoMapper.selectPageLikeName(page,gender,healthStatus,name);
        for (ElderInfo elderInfo : elderInfoPage.getRecords()) {
            //caregiverInfo查询
            CaregiverInfo caregiver = caregiverInfoMapper.selectById(elderInfo.getCaregiverId());
            Employee caregiverEmployee = employeeMapper.selectById(elderInfo.getCsManagerId());
            elderInfo.setCaregiver(caregiver);
            elderInfo.setCsManager(caregiverEmployee);
        }
        //convert方法转换Page中 records集合元素类型 Admin---->AdminView
        //new Function<Admin,AdminView>(){};表示创建一个实现Function的匿名类 对象
        return JsonResult.success(elderInfoPage);
    }
    @Override
    public JsonResult deleteById(Long id) {
        int rows=elderInfoMapper.deleteById(id);
        if (rows>0){
            return JsonResult.success("删除成功");
        }
        else {
            return JsonResult.fail("删除失败");
        }
    }
    //@Transactional用在service方法上，保证事务
    //注意：默认情况@Transactional只对运行时异常起作用(RuntimeException)
    @Transactional
    @Override
    public JsonResult save(ElderInfo elderInfoParam) {
        ElderInfo elderInfo=null;
        LocalDateTime now=LocalDateTime.now();
        //通过判断id值有无判断是新增还是更新，id值无是新增，id值有就是更新
        if(elderInfoParam.getElderlyId()==null||elderInfoParam.getElderlyId()<=0){
            //新增
            elderInfo=new ElderInfo();
            BeanUtils.copyProperties(elderInfoParam,elderInfo);
            elderInfo.setCreateTime(now);
            elderInfo.setUpdateTime(now);
            elderInfoMapper.insert(elderInfo);
            return JsonResult.success("新增成功",null);
        }else{
            //更新
            //根据id查询Admin信息
            elderInfo=elderInfoMapper.selectById(elderInfoParam.getElderlyId());
            if(elderInfo==null){
                return JsonResult.fail("用户不存在，更新失败");
            }
            //要更新的字段
            elderInfo.setName(elderInfoParam.getName());
            elderInfo.setUpdateTime(now);
            elderInfo.setCaregiver(elderInfoParam.getCaregiver());
            elderInfo.setAge(elderInfoParam.getAge());
            elderInfo.setCaregiverId(elderInfoParam.getCaregiverId());
            elderInfo.setCsManager(elderInfoParam.getCsManager());
            elderInfo.setCsManagerId(elderInfoParam.getCsManagerId());
            elderInfo.setGender(elderInfoParam.getGender());
            elderInfo.setHealthStatus(elderInfoParam.getHealthStatus());
            elderInfo.setIsDeleted(elderInfoParam.getIsDeleted());
            int rows=elderInfoMapper.updateById(elderInfo);
            if (rows>0){
                return JsonResult.success("更新成功",null);
            }else {
                return JsonResult.fail("用户不存在，更新失败");
            }
        }
    }
    @Override
    public JsonResult getElderList(){
        List<Map<String, Object>> ElderList =elderInfoMapper.getElderList();
        return JsonResult.success(ElderList);
    }
}
